﻿

Imports System.Object
Imports VSModel
Imports Cls_Common
Imports System.IO
Imports System.Data
Partial Class FrontEnd_Control_RaoVat_Preview
    Inherits System.Web.UI.UserControl
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            If (Not Page.RouteData.Values("Id") Is Nothing) Then

                hidID.Value = Page.RouteData.Values("Id")
                ShowData()
                ShowThongTinCoBan()
                ShowTienNghi()
                ShowThongSoKyThuat()
                ShowAnToan()

                LoadTinhThanh()
                BindToGrid(1, ddlTinhThanh.SelectedValue)
                BindToGrid1(1, ddlTinhThanh.SelectedValue)
                BindCungNguoiDang(1)

            End If
        End If
    End Sub
 
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim intRaoVat As Integer = CInt(hidID.Value.Trim)
            Dim p As uspRaovatSelect_Result = data.uspRaovatSelect(intRaoVat).FirstOrDefault
            If Not p Is Nothing Then
                Page.Title = "Xe " & p.Title & "  giá " & IIf(p.OriginalPrice = 0, "Call", FormatCurrencyOto(p.OriginalPrice)) & " | OtoSaiThanh.com"
                Page.MetaDescription = p.Description.Replace("\n", " ")


                Dim _USD As String = ""

                If Not p.SellPrice Is Nothing AndAlso p.SellPrice <> 0 Then
                    _USD = "(~" & Format(CType(p.SellPrice, Double), "#,##0") & " USD)"
                End If
                Page.MetaKeywords = "cần bán xe " & p.Title & "  giá " & IIf(p.OriginalPrice = 0, "Call", FormatCurrencyOto(p.OriginalPrice)) & "(" & _USD & ")"


                ltrTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title) & " <span>" & IIf(p.OriginalPrice = 0, "Call", FormatCurrencyOto(p.OriginalPrice)) _
                            & "</span><span class='USD'> " & _USD & "</span>"

                hidImagePath.Text = IIf(IsNothing(p.ImagePreview) = True, "", p.ImagePreview)
                lblHoTen.Text = IIf(IsNothing(p.HoTen) = True, "", p.HoTen)
                ltrDiachi.Text = IIf(IsNothing(p.DiaChi) = True, "", p.DiaChi) & " " & IIf(IsNothing(p.TinhTP), "", ", " & p.TinhTP)
                ltrDienThoai.Text = IIf(IsNothing(p.Phone) = True, "", p.Phone)
                If Not p.PublishedDate Is Nothing Then
                    ltrDatePost.Text = "Đăng ngày " & CType(p.PublishedDate, Date).ToString("dd/MM/yyyy")
                End If

                If Not p.PhoneOther Is Nothing Then
                    ltrDienThoai.Text = ltrDienThoai.Text & " - " & p.PhoneOther
                End If

                Dim t = (From a In data.RaovaImages Where a.RaoVatId = p.RaovatId Select a).Take(10).ToList
                If Not t Is Nothing AndAlso t.Count > 1 Then

                    rptBigBanner.DataSource = t
                    rptBigBanner.DataBind()

                    rptImageThumb.DataSource = t
                    rptImageThumb.DataBind()
                End If

                Dim k = (From q In data.RaoVats Where q.RaovatId = intRaoVat).FirstOrDefault

                If Not k Is Nothing Then
                    If (Session("PageViewRaoVat") Is Nothing) Then
                        Session("PageViewRaoVat") = Now
                        'Update so luot xem len 1
                        If Not k.Viewed Is Nothing Then
                            k.Viewed = k.Viewed + 1
                        Else
                            k.Viewed = 10
                        End If
                        ' k.Viewed = IIf(IsNothing(k.Viewed) = True, 1, k.Viewed + 1)
                        data.SaveChanges()
                    Else
                        If (DateDiff(DateInterval.Second, CType(Session("PageViewRaoVat"), Date), Now()) >= ConfigurationManager.AppSettings("SettingTimeUpdatePageView")) Then
                            'Update so luot xem len 1
                            If Not k.Viewed Is Nothing Then
                                k.Viewed = k.Viewed + 1
                            Else
                                k.Viewed = 10
                            End If
                            data.SaveChanges()
                            Session("PageViewRaoVat") = Now
                        End If
                    End If

                    ltrView.Text = ". Lượt xem: " & k.Viewed

                End If
            End If
        End Using
    End Sub

    Protected Sub ShowThongTinCoBan()
        Using data As New VSEntities
            Dim p As uspThongsocobanSelect_Result = data.uspThongsocobanSelect(hidID.Value).FirstOrDefault
            If Not p Is Nothing Then
                'lbl = IIf(IsNothing(p.XuatXu) = True, "", p.XuatXu)
                lblTinhTrang.Text = IIf(IsNothing(p.TinhTrang) = True, "", p.TinhTrang)
                lblDongXe.Text = IIf(IsNothing(p.DongXe) = True, "", p.DongXe)
                lblSoKm.Text = IIf(IsNothing(p.SoKLMDaDi) = True, "", p.SoKLMDaDi)
                lblMauNgoaiThat.Text = IIf(IsNothing(p.MauNgoaiThat) = True, "", p.MauNgoaiThat)
                lblMauNoiThat.Text = IIf(IsNothing(p.MauNoiThat) = True, "", p.MauNoiThat)
                lblSocua.Text = IIf(IsNothing(p.SoCua) = True, "", p.SoCua)
                lblSochongoi.Text = IIf(IsNothing(p.SoChoNgoi) = True, "", p.SoChoNgoi.ToString)
                lblNhienLieu.Text = IIf(IsNothing(p.NhienLieu) = True, "", p.NhienLieu)
                lblHeThongNhienLieu.Text = IIf(IsNothing(p.HeThongNhienLieu) = True, "", p.HeThongNhienLieu)
                lblHopSo.Text = IIf(IsNothing(p.HopSo) = True, "", p.HopSo)
                lblDanDong.Text = IIf(IsNothing(p.DanDong) = True, "", p.DanDong)
                lblTieuthunhienlieu.Text = IIf(IsNothing(p.TieuThuNhienLieu) = True, "", p.TieuThuNhienLieu)
                lblThietBiKhacCoBan.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                lblHang.Text = IIf(IsNothing(p.HangCheTao) = True, "", p.HangCheTao)
                lblDoixe.Text = IIf(IsNothing(p.DoiXe) = True, "", p.DoiXe)
                lblNamsanxuat.Text = IIf(IsNothing(p.NamSanXuat) = True, "", p.NamSanXuat)
                


            End If
        End Using
    End Sub

    Protected Sub ShowAnToan()
        Using data As New VSEntities
            Dim p As AnToan = (From q In data.AnToans Where q.RaoVatId = hidID.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                rptTuiKhiNguoiLai.Checked = IIf(IsNothing(p.TuiKhiNguoiLai) = True, False, p.TuiKhiNguoiLai)
                rptTuiKhiTruoc.Checked = IIf(IsNothing(p.TuiKhiTruoc) = True, False, p.TuiKhiTruoc)
                rptTuiKhisau.Checked = IIf(IsNothing(p.TuiKhiSau) = True, False, p.TuiKhiSau)
                rptHaiBenGhe.Checked = IIf(IsNothing(p.TuiKhiHaiBenGhe) = True, False, p.TuiKhiHaiBenGhe)
                rptABS.Checked = IIf(IsNothing(p.PhanhABS) = True, False, p.PhanhABS)
                ' rpt.Checked = IIf(IsNothing(p.PhanBoLucABD) = True, False, p.PhanBoLucABD)
                rptEBA.Checked = IIf(IsNothing(p.TroLucPhanhEBA) = True, False, p.TroLucPhanhEBA)
                rptESB.Checked = IIf(IsNothing(p.CanBangDienTuESP) = True, False, p.CanBangDienTuESP)
                rptDKHanhTrinh.Checked = IIf(IsNothing(p.DieuKhienHanhTrinh) = True, False, p.DieuKhienHanhTrinh)
                rptCanhBaoLui.Checked = IIf(IsNothing(p.HoTroCanhBaoLui) = True, False, p.HoTroCanhBaoLui)
                rptKiemSoatTruot.Checked = IIf(IsNothing(p.KiemSoatTruot) = True, False, p.KiemSoatTruot)
                rptChotAntoan.Checked = IIf(IsNothing(p.ChotCuaAnToan) = True, False, p.ChotCuaAnToan)
                rptKhoaCuaTuDong.Checked = IIf(IsNothing(p.KhoaCuaTuDong) = True, False, p.KhoaCuaTuDong)
                rptKhoaCuaDKTuXa.Checked = IIf(IsNothing(p.KhoaCuaDKTuXa) = True, False, p.KhoaCuaDKTuXa)
                rptKhoaDongCo.Checked = IIf(IsNothing(p.KhoaDongCo) = True, False, p.KhoaDongCo)
                rptChongTrom.Checked = IIf(IsNothing(p.HTBaoNgoaiVi) = True, False, p.HTBaoNgoaiVi)
                rptDenSuongMu.Checked = IIf(IsNothing(p.DenSuongMu) = True, False, p.DenSuongMu)
                rptThatDayAnToan.Checked = IIf(IsNothing(p.DenDayAnToan) = True, False, p.DenDayAnToan)
                rptDenPhu.Checked = IIf(IsNothing(p.DenPhanhPhu3) = True, False, p.DenPhanhPhu3)

            End If
        End Using
    End Sub

    Protected Sub ShowTienNghi()
        Using data As New VSEntities
            Dim p As TienNghi = (From q In data.TienNghis Where q.RoaVatId = hidID.Value Select q).FirstOrDefault
            If Not p Is Nothing Then
                rptThietBiDinhVi.Checked = IIf(IsNothing(p.ThietBiDinhVi) = True, False, p.ThietBiDinhVi)
                rptCuaSoNoc.Checked = IIf(IsNothing(p.CuaSoNoc) = True, False, p.CuaSoNoc)
                rptKinhChinhDien.Checked = IIf(IsNothing(p.KinhChinhDien) = True, False, p.KinhChinhDien)
                rptTayLaiTroLuc.Checked = IIf(IsNothing(p.TayLaiTroLuc) = True, False, p.TayLaiTroLuc)
                lblGheChatLieu.Text = IIf(IsNothing(p.GheChatLieuTienNghi) = True, False, p.GheChatLieuTienNghi)
                rptDieuHoaTruoc.Checked = IIf(IsNothing(p.DieuHoaTruoc) = True, False, p.DieuHoaTruoc)
                rptDieuHoaSau.Checked = IIf(IsNothing(p.DieuHoaSau) = True, False, p.DieuHoaSau)
                rptHoTroXeTuDong.Checked = IIf(IsNothing(p.HoTroXeTuDong) = True, False, p.HoTroXeTuDong)
                rptSaykinhsau.Checked = IIf(IsNothing(p.SayKinhSau) = True, False, p.SayKinhSau)
                rptQuatKinhSau.Checked = IIf(IsNothing(p.QuatKinhPhiaSau) = True, False, p.QuatKinhPhiaSau)
                rptKinhMau.Checked = IIf(IsNothing(p.KinhMau) = True, False, p.KinhMau)
                rptLCD.Checked = IIf(IsNothing(p.ManHinhLCD) = True, False, p.ManHinhLCD)
                rptGiaiTri.Checked = IIf(IsNothing(p.ThietBiGiaiTri) = True, False, p.ThietBiGiaiTri)
                lblThietBiKhacTienNghi.Text = IIf(IsNothing(p.ThietBiKhac) = True, "", p.ThietBiKhac)

            End If
        End Using
    End Sub

    Protected Sub ShowThongSoKyThuat()
        Using data As New VSEntities
            Dim p As uspThongsokythuatSelect_Result = data.uspThongsokythuatSelect(hidID.Value).FirstOrDefault
            If Not p Is Nothing Then
                lblDaiRongCao.Text = IIf(IsNothing(p.DaiRongCao) = True, "", p.DaiRongCao)
                lblChieudaicoso.Text = IIf(IsNothing(p.ChieuDaiCoSo) = True, "", p.ChieuDaiCoSo)
                lblChieurongcoso.Text = IIf(IsNothing(p.ChieuRongCoSo) = True, "", p.ChieuRongCoSo)
                lblTrongluongkhongtai.Text = IIf(IsNothing(p.TrongLuongKhongTai) = True, "", p.TrongLuongKhongTai)
                lblDungtichbinhnhienlieu.Text = IIf(IsNothing(p.DungTichBinhNhienLieu) = True, "", p.DungTichBinhNhienLieu)
                lblDongCo.Text = IIf(IsNothing(p.DongCo) = True, "", p.DongCo)
                lblKieuDongCo.Text = IIf(IsNothing(p.KieuDongCo) = True, "", p.KieuDongCo)
                lblDungtichxilanh.Text = IIf(IsNothing(p.DungTichXiLanh) = True, "", p.DungTichXiLanh)
                lblPhanh.Text = IIf(IsNothing(p.Phanh) = True, "", p.Phanh)
                lblGiamSoc.Text = IIf(IsNothing(p.GiamSoc) = True, "", p.GiamSoc)
                lblLopXe.Text = IIf(IsNothing(p.LopXe) = True, "", p.LopXe)
                lblVanhMam.Text = IIf(IsNothing(p.VanhMam) = True, "", p.VanhMam)
                lblThietBiKhacKyThuat.Text = IIf(IsNothing(p.ThongSoKhac) = True, "", p.ThongSoKhac)
            End If
        End Using
    End Sub

    

    Protected Sub rptImageThumb_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptImageThumb.ItemDataBound
        Dim row As RaovaImage = e.Item.DataItem
        Dim imgThumb As Image = e.Item.FindControl("imgThumb")
        imgThumb.ImageUrl = CookedImageURL("90-60", row.ImageURL)
        imgThumb.AlternateText = row.Description


    End Sub

    Protected Sub rptBigBanner_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptBigBanner.ItemDataBound
        Dim row As RaovaImage = e.Item.DataItem
        Dim HighslideImage1 As Encosia.HighslideImage = e.Item.FindControl("HighslideImage1")
        HighslideImage1.ImageUrl = CookedImageURL("420-300", row.ImageURL)
        HighslideImage1.FullImageURL = CookedImageURL("800-600", row.ImageURL)
        HighslideImage1.AlternateText = row.Description
        If e.Item.ItemIndex = 0 Then
            HighslideImage1.Attributes.Add("style", "display:inline")
        Else
            HighslideImage1.Attributes.Add("style", "display:none")
        End If

    End Sub

#Region "tin khac"
    Protected Sub LoadTinhThanh()
        Using Data As New VSEntities
            Dim p = (From q In Data.KhuVucs Select q).ToList
            For Each a In p
                'insert khu vuc
                Dim itm As New ListItem
                itm.Text = a.Name
                itm.Value = 0
                itm.Attributes.Add("disabled", "disabled")
                itm.Attributes.Add("class", "Khuvuc")
                ddlTinhThanh.Items.Add(itm)

                Dim _khuvucId = a.Id
                Dim k = (From q In Data.DonViHanhChinhs Where q.KhuVucId = _khuvucId Order By q.Title Ascending Select q).ToList
                For Each item In k
                    Dim itm1 As New ListItem
                    itm1.Value = item.Id
                    itm1.Text = "---" & item.Title
                    itm1.Enabled = True
                    ddlTinhThanh.Items.Add(itm1)
                Next
            Next
        End Using
    End Sub

    Private Sub BindToGrid(ByVal ipage As Integer, ByVal _TinhThanhId As Integer)
        Using Data As New VSEntities
            Dim count As Integer
            Dim intID As Integer = hidID.Value
            Dim h = (From k In Data.RaoVats Where k.RaovatId = intID Select k).FirstOrDefault
            Dim p As List(Of RaoVat)
            p = (From q In Data.RaoVats Where ((q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0)) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) And (q.CateID = h.CateID) _
                                                And (q.RaovatId <> intID) _
                                                Order By q.Modified Descending, q.Created Descending Select q).Skip((ipage - 1) * 10).Take(10).ToList

            count = (From q In Data.RaoVats Where (q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0) And q.StatusId = 3 _
                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                            And (q.RaovatId <> intID) _
                                            Or q.eEffectiveDate Is Nothing) And (q.CateID = h.CateID) Select q.RaovatId).Count

            'cung model
            If count > 0 Then
                hidCount.Value = count
                Create_Pager(hidCount.Value, ipage, 10, 10)
            Else
                hidCount.Value = 0
                With rptPage
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With Repeater1
                .DataSource = p
                .DataBind()
            End With

        End Using
    End Sub
    Private Sub BindToGrid1(ByVal ipage As Integer, ByVal _TinhThanhId As Integer)
        Using Data As New VSEntities
            'cung gia
            Dim countCungGia As Integer
            Dim intID As Integer = hidID.Value
            Dim h = (From t In Data.RaoVats Where t.RaovatId = intID Select t).FirstOrDefault
            Dim intGia As Integer = 0
            If Not h Is Nothing Then
                intGia = h.OriginalPrice
            End If

            Dim k As List(Of RaoVat)
            k = (From q In Data.RaoVats Where ((q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0)) And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                Or q.eEffectiveDate Is Nothing) _
                                                And (q.OriginalPrice >= (intGia - intGia * 0.1) And q.OriginalPrice <= (intGia + intGia * 0.1)) _
                                                And (q.RaovatId <> intID) _
                                                Order By q.Modified Descending, q.Created Descending Select q).Skip((ipage - 1) * 10).Take(10).ToList

            countCungGia = (From q In Data.RaoVats Where (q.User.TinhTP = _TinhThanhId Or _TinhThanhId = 0) And q.StatusId = 3 _
                                            And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) _
                                           And (q.OriginalPrice >= (intGia - intGia * 0.1) And q.OriginalPrice <= (intGia + intGia * 0.1)) _
                                            And (q.RaovatId <> intID) _
                                             Select q.RaovatId).Count

            'cung model
            If countCungGia > 0 Then
                hidCount1.Value = countCungGia
                Create_Pager1(hidCount1.Value, ipage, 10, 10)
            Else
                hidCount1.Value = 0
                With rptPage1
                    .DataSource = Nothing
                    .DataBind()
                End With
            End If

            With rptCungGiaBan
                .DataSource = k
                .DataBind()
            End With
        End Using
    End Sub

    Private Sub BindCungNguoiDang(ByVal ipage As Integer)
        Using Data As New VSEntities
            'cung gia
            Dim _NguoiDang As Integer = (From q In Data.RaoVats Where q.RaovatId = hidID.Value Select q.CreatorId).FirstOrDefault
            Dim _checkNguoiDang As Integer = 0
            Dim _user = (From q In Data.Users Where q.UserId = _NguoiDang Select q.Vip).FirstOrDefault
            If Not _user Is Nothing Then
                _checkNguoiDang = _user
            End If
            Dim salon = (From q In Data.Stores Where q.UserId = _NguoiDang Select q).FirstOrDefault
            If Not salon Is Nothing Then
                ltrSalonName.Text = salon.Title
                ltrWebsite.Text = "<br>Website: <a href='" & MainDomain & "salon-oto/" & salon.QueryString & "-" & salon.StoreId.ToString & ".html'>" & MainDomain & "salon-oto/" & salon.QueryString & "-" & salon.StoreId.ToString & ".html </a>"
            End If

            'neu la vip va co salon
            If _checkNguoiDang = 3 AndAlso Not salon Is Nothing Then
                Dim countNguoiDang As Integer
                Dim intID As Integer = hidID.Value
                Dim h = (From t In Data.RaoVats Where t.RaovatId = intID Select t).FirstOrDefault
                Dim intGia As Integer = 0
                If Not h Is Nothing Then
                    intGia = h.OriginalPrice
                End If

                Dim k As List(Of RaoVat)
                k = (From q In Data.RaoVats Where q.CreatorId = _NguoiDang And q.StatusId = 3 _
                                                    And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() _
                                                    Or q.eEffectiveDate Is Nothing) _
                                                    And (q.RaovatId <> intID) _
                                                    Order By q.Modified Descending, q.Created Descending Select q).Skip((ipage - 1) * 10).Take(10).ToList

                countNguoiDang = (From q In Data.RaoVats Where q.CreatorId = _NguoiDang And q.StatusId = 3 _
                                                And q.PublishedDate <= Now() And (q.eEffectiveDate >= Now() Or q.eEffectiveDate Is Nothing) _
                                                And q.RaovatId <> intID _
                                                 Select q.RaovatId).Count

                'cung model
                If countNguoiDang > 0 Then
                    hidCountNguoiDang.Value = countNguoiDang
                    Create_PagerNguoiDang(hidCountNguoiDang.Value, ipage, 10, 10)
                Else
                    hidCountNguoiDang.Value = 0
                    With rptPageNguoiDang
                        .DataSource = Nothing
                        .DataBind()
                    End With
                End If

                With rptNguoiDang
                    .DataSource = k
                    .DataBind()
                End With
            Else
                Excute_Javascript("ShowXeCungModel();", Me.Page)

            End If

           
        End Using
    End Sub



#End Region
#Region "Event for control"

    Sub Create_Pager(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = e.Item.FindControl("lnkTitle")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        BindToGrid(lnkTile.ToolTip, ddlTinhThanh.SelectedValue)
        lnkLast.Enabled = True
        lnkFirst.Enabled = True
        If CInt(lnkTile.ToolTip) = hidIndex_page.Value Then
            lnkLast.Enabled = False
        ElseIf CInt(lnkTile.ToolTip) = 1 Then
            lnkFirst.Enabled = False
        End If
    End Sub

    Protected Sub lnkFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst.Click
        If hidCur_Page.Value > 1 Then
            hidCur_Page.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub

    Protected Sub lnkLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast.Click
        hidCur_Page.Value = hidCur_Page.Value + 1
        BindToGrid(hidCur_Page.Value, ddlTinhThanh.SelectedValue)
    End Sub

    Protected Sub lbtTitle_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbtTitle As LinkButton = CType(sender, LinkButton)
        ddlTinhThanh.SelectedValue = lbtTitle.ToolTip
        BindToGrid(1, lbtTitle.ToolTip)

    End Sub

    Protected Sub rptShow_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"

        Dim lblTinhTrang As Label = CType(e.Item.FindControl("lblTinhTrang"), Label)

        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        img.ImageUrl = CookedImageURL("84-84", row.ImagePreview.ToString)
        img.AlternateText = row.Title
        Dim ltrThongSo As Literal = CType(e.Item.FindControl("ltrThongSo"), Literal)

        Dim ltrMota As Literal = CType(e.Item.FindControl("ltrMota"), Literal)

        Dim ltrKhuVuc As Literal = CType(e.Item.FindControl("ltrKhuVuc"), Literal)
        Using Data As New VSEntities
            Dim p As uspThongsocobanSelect_Result = Data.uspThongsocobanSelect(row.RaovatId).FirstOrDefault
            If Not p Is Nothing Then
                ltrThongSo.Text = p.XuatXu & "," & p.MauNgoaiThat & "," & p.HeThongNhienLieu & "," & p.HopSo
                lblTinhTrang.Text = "[" & p.TinhTrang & "]"
                ltrMota.Text = CropText(p.Description, 200, "...")
            End If
            Dim h = (From l In Data.DonViHanhChinhs Where l.Id = row.TinhTP Select l).FirstOrDefault
            If Not h Is Nothing Then
                ltrKhuVuc.Text = h.Title
            End If

        End Using
        Dim ltrGiaBan As Literal = CType(e.Item.FindControl("ltrGiaBan"), Literal)
        '     ltrGiaBan.Text = FormatCurrencyOto(IIf(IsNothing(row.OriginalPrice), 0, row.OriginalPrice)) & " triệu"
        If row.OriginalPrice = 0 Then
            ltrGiaBan.Text = "Call"
        Else
            ltrGiaBan.Text = IIf(row.OriginalPrice = 0, 0, FormatCurrencyOto(row.OriginalPrice.ToString)) & " triệu"
        End If


        Dim ltrGiaTuongDuong As Literal = CType(e.Item.FindControl("ltrGiaTuongDuong"), Literal)

        If Not row.SellPrice Is Nothing Then
            ltrGiaTuongDuong.Text = "~ " & CType(row.SellPrice.ToString, Double).ToString("#,##0") & " USD"
        End If

        'Dim _tygia As Integer = (row.OriginalPrice * 1000000) / TyGia
        'ltrGiaTuongDuong.Text = "~" & _tygia & " USD"

        Dim lblHoTen As Label = CType(e.Item.FindControl("lblHoTen"), Label)
        Dim ltrDienThoai As Literal = CType(e.Item.FindControl("ltrDienThoai"), Literal)
        Dim ltrDiaChi As Literal = CType(e.Item.FindControl("ltrDiaChi"), Literal)

        lblHoTen.Text = IIf(IsNothing(row.HoTen) = True, "", row.HoTen) & Environment.NewLine
        ltrDiaChi.Text = IIf(IsNothing(row.DiaChi) = True, "", row.DiaChi) & ", " & ltrKhuVuc.Text & vbNewLine
        ltrDienThoai.Text = IIf(IsNothing(row.Phone) = True, "", row.Phone)

        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrDienThoai.Text = ltrDienThoai.Text & " - " & row.PhoneOther
        End If

    End Sub

    Protected Sub ddlTinhThanh_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlTinhThanh.SelectedIndexChanged
        BindToGrid(1, ddlTinhThanh.SelectedValue)
        BindToGrid1(1, ddlTinhThanh.SelectedValue)
    End Sub

#End Region
#Region "Tin cung gia"

    Sub Create_Pager1(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_page1.Value = TotalPage
        'gan lai curPage de set active
        hidCur_Page1.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPage1
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub

    Protected Sub rptPage1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPage1.ItemDataBound
        Dim lnkTitle1 As LinkButton
        lnkTitle1 = e.Item.FindControl("lnkTitle1")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle1)
        If e.Item.DataItem = hidCur_Page.Value Then
            lnkTitle1.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitle1.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitle1.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitle1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile1 As LinkButton = CType(sender, LinkButton)
        BindToGrid1(lnkTile1.ToolTip, ddlTinhThanh.SelectedValue)
        lnkLast1.Enabled = True
        lnkFirst1.Enabled = True
        If CInt(lnkTile1.ToolTip) = hidIndex_page.Value Then
            lnkLast1.Enabled = False
        ElseIf CInt(lnkTile1.ToolTip) = 1 Then
            lnkFirst1.Enabled = False
        End If
    End Sub

    Protected Sub lnkFirst1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirst1.Click
        If hidCur_Page1.Value > 1 Then
            hidCur_Page1.Value = hidCur_Page.Value - 1
        End If
        BindToGrid(hidCur_Page1.Value, ddlTinhThanh.SelectedValue)
    End Sub

    Protected Sub lnkLast1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLast1.Click
        hidCur_Page1.Value = hidCur_Page1.Value + 1
        BindToGrid1(hidCur_Page1.Value, ddlTinhThanh.SelectedValue)
    End Sub

    Protected Sub lbtTitle1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbtTitle1 As LinkButton = CType(sender, LinkButton)
        ddlTinhThanh.SelectedValue = lbtTitle1.ToolTip
        BindToGrid1(1, lbtTitle1.ToolTip)
    End Sub

    Protected Sub rptCungGiaBan_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptCungGiaBan.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"

        Dim lblTinhTrang As Label = CType(e.Item.FindControl("lblTinhTrang"), Label)

        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        img.ImageUrl = CookedImageURL("84-84", row.ImagePreview.ToString)
        img.AlternateText = row.Title
        Dim ltrThongSo As Literal = CType(e.Item.FindControl("ltrThongSo"), Literal)

        Dim ltrMota As Literal = CType(e.Item.FindControl("ltrMota"), Literal)

        Dim ltrKhuVuc As Literal = CType(e.Item.FindControl("ltrKhuVuc"), Literal)
        Using Data As New VSEntities
            Dim p As uspThongsocobanSelect_Result = Data.uspThongsocobanSelect(row.RaovatId).FirstOrDefault
            If Not p Is Nothing Then
                ltrThongSo.Text = p.XuatXu & "," & p.MauNgoaiThat & "," & p.HeThongNhienLieu & "," & p.HopSo
                lblTinhTrang.Text = "[" & p.TinhTrang & "]"
                ltrMota.Text = CropText(p.Description, 200, "...")
            End If
            Dim h = (From l In Data.DonViHanhChinhs Where l.Id = row.TinhTP Select l).FirstOrDefault
            If Not h Is Nothing Then
                ltrKhuVuc.Text = h.Title
            End If

        End Using
        Dim ltrGiaBan As Literal = CType(e.Item.FindControl("ltrGiaBan"), Literal)
        'ltrGiaBan.Text = FormatCurrencyOto(IIf(IsNothing(row.OriginalPrice), 0, row.OriginalPrice)) & " triệu"
        If row.OriginalPrice = 0 Then
            ltrGiaBan.Text = "Call"
        Else
            ltrGiaBan.Text = IIf(row.OriginalPrice = 0, 0, FormatCurrencyOto(row.OriginalPrice.ToString)) & " triệu"
        End If


        Dim ltrGiaTuongDuong As Literal = CType(e.Item.FindControl("ltrGiaTuongDuong"), Literal)

        If Not row.SellPrice Is Nothing Then
            ltrGiaTuongDuong.Text = "~ " & CType(row.SellPrice.ToString, Double).ToString("#,##0") & " USD"
        End If

        Dim lblHoTen As Label = CType(e.Item.FindControl("lblHoTen"), Label)
        Dim ltrDienThoai As Literal = CType(e.Item.FindControl("ltrDienThoai"), Literal)
        Dim ltrDiaChi As Literal = CType(e.Item.FindControl("ltrDiaChi"), Literal)

        lblHoTen.Text = IIf(IsNothing(row.HoTen) = True, "", row.HoTen) & Environment.NewLine
        ltrDiaChi.Text = IIf(IsNothing(row.DiaChi) = True, "", row.DiaChi) & ", " & ltrKhuVuc.Text & vbNewLine
        ltrDienThoai.Text = IIf(IsNothing(row.Phone) = True, "", row.Phone)

        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrDienThoai.Text = ltrDienThoai.Text & " - " & row.PhoneOther
        End If

    End Sub


    Protected Sub rptNguoiDang_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptNguoiDang.ItemDataBound
        Dim row As VSModel.RaoVat = CType(e.Item.DataItem, VSModel.RaoVat)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = row.Title
        hplTitle.NavigateUrl = MainDomain & row.QueryString & "-" & row.RaovatId.ToString & ".html"

        Dim lblTinhTrang As Label = CType(e.Item.FindControl("lblTinhTrang"), Label)

        Dim img As Image = CType(e.Item.FindControl("img"), Image)
        img.ImageUrl = CookedImageURL("84-84", row.ImagePreview.ToString)
        img.AlternateText = row.Title

        Dim ltrThongSo As Literal = CType(e.Item.FindControl("ltrThongSo"), Literal)

        Dim ltrMota As Literal = CType(e.Item.FindControl("ltrMota"), Literal)

        Dim ltrKhuVuc As Literal = CType(e.Item.FindControl("ltrKhuVuc"), Literal)
        Using Data As New VSEntities
            Dim p As uspThongsocobanSelect_Result = Data.uspThongsocobanSelect(row.RaovatId).FirstOrDefault
            If Not p Is Nothing Then
                ltrThongSo.Text = p.XuatXu & "," & p.MauNgoaiThat & "," & p.HeThongNhienLieu & "," & p.HopSo
                lblTinhTrang.Text = "[" & p.TinhTrang & "]"
                ltrMota.Text = CropText(p.Description, 200, "...")
            End If
            Dim h = (From l In Data.DonViHanhChinhs Where l.Id = row.TinhTP Select l).FirstOrDefault
            If Not h Is Nothing Then
                ltrKhuVuc.Text = h.Title
            End If

        End Using
        Dim ltrGiaBan As Literal = CType(e.Item.FindControl("ltrGiaBan"), Literal)
        'ltrGiaBan.Text = FormatCurrencyOto(IIf(IsNothing(row.OriginalPrice), 0, row.OriginalPrice)) & " triệu"
        If row.OriginalPrice = 0 Then
            ltrGiaBan.Text = "Call"
        Else
            ltrGiaBan.Text = IIf(row.OriginalPrice = 0, 0, FormatCurrencyOto(row.OriginalPrice.ToString)) & " triệu"
        End If


        Dim ltrGiaTuongDuong As Literal = CType(e.Item.FindControl("ltrGiaTuongDuong"), Literal)

        If Not row.SellPrice Is Nothing Then
            ltrGiaTuongDuong.Text = "~ " & CType(row.SellPrice.ToString, Double).ToString("#,##0") & " USD"
        End If

        Dim lblHoTen As Label = CType(e.Item.FindControl("lblHoTen"), Label)
        Dim ltrDienThoai As Literal = CType(e.Item.FindControl("ltrDienThoai"), Literal)
        Dim ltrDiaChi As Literal = CType(e.Item.FindControl("ltrDiaChi"), Literal)

        lblHoTen.Text = IIf(IsNothing(row.HoTen) = True, "", row.HoTen) & Environment.NewLine
        ltrDiaChi.Text = IIf(IsNothing(row.DiaChi) = True, "", row.DiaChi) & ", " & ltrKhuVuc.Text & vbNewLine
        ltrDienThoai.Text = IIf(IsNothing(row.Phone) = True, "", row.Phone)

        If Not row.PhoneOther Is Nothing AndAlso row.PhoneOther <> "" Then
            ltrDienThoai.Text = ltrDienThoai.Text & " - " & row.PhoneOther
        End If

    End Sub

    Sub Create_PagerNguoiDang(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndex_pageNguoiDang.Value = TotalPage
        'gan lai curPage de set active
        hidCur_PageNguoiDang.Value = Page_Index
        'generate ra left page
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPageNguoiDang
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub


    Protected Sub rptPageNguoiDang_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPageNguoiDang.ItemDataBound
        Dim lnkTitleNguoiDang As LinkButton
        lnkTitleNguoiDang = e.Item.FindControl("lnkTitleNguoiDang")
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitleNguoiDang)
        If e.Item.DataItem = hidCur_PageNguoiDang.Value Then
            lnkTitleNguoiDang.Text = "<span class='currentpage'>" & e.Item.DataItem & "</span>"
        Else
            lnkTitleNguoiDang.Text = "<span>" & e.Item.DataItem & "</span>"
        End If
        lnkTitleNguoiDang.ToolTip = e.Item.DataItem
    End Sub

    Protected Sub lnkTitleNguoiDang_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lbtTitle1 As LinkButton = CType(sender, LinkButton)
        ddlTinhThanh.SelectedValue = lbtTitle1.ToolTip
        BindCungNguoiDang(lbtTitle1.ToolTip)
    End Sub

    Protected Sub lnkFirstNguoiDang_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirstNguoiDang.Click
        If hidCur_PageNguoiDang.Value > 1 Then
            hidCur_PageNguoiDang.Value = hidCur_PageNguoiDang.Value - 1
        End If
        BindCungNguoiDang(hidCur_PageNguoiDang.Value)
    End Sub

    Protected Sub lnkLastNguoiDang_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLastNguoiDang.Click
        hidCur_PageNguoiDang.Value = hidCur_PageNguoiDang.Value + 1
        BindCungNguoiDang(hidCur_PageNguoiDang.Value)
    End Sub

#End Region
End Class
